<?php


namespace app\admin\controller\tickets;
use app\common\controller\Adminbase;
use think\Db;

class Package extends Adminbase
{
    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
        $ticket_category = Db::name('ticket_category')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        $this->assign('ticket_category', $ticket_category);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $_list = Db::name('water_ticket')
                ->alias('wt')
                ->field('wt.*,tc.name as tcname')
                ->join('ticket_category tc','wt.tcategory_id=tc.id')
                ->where('tc.is_del','<>',1)
                ->where('wt.is_del','<>',1)
                ->page($page, $limit)
                ->order('tc.sort desc')
                ->order('wt.count desc')
                ->select();
            $total=Db::name('water_ticket')
                ->alias('wt')
                ->field('wt.*,tc.name as tcname')
                ->join('ticket_category tc','wt.tcategory_id=tc.id')
                ->where('tc.is_del','<>',1)
                ->where('wt.is_del','<>',1)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('water_ticket')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'price' => $data['price'],
                'rebate' => $data['rebate'],
                'name' => $data['name'],
                'sort' => $data['sort'],
                'count1'=>$data['count1'],
                'git_count'=>$data['git_count'],
                'count' => $data['count1']+$data['git_count'],
                'tcategory_id' => $data['tcategory_id'],
            );
            $stmt = Db::name('water_ticket')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data=$this->request->Post();
            $update_data = array(
                'id'=>$data['id'],
                'name' => $data['name'],
                'price' => $data['price'],
                'rebate'=>$data['rebate'],
                'sort'=>$data['sort'],
                'count1'=>$data['count1'],
                'git_count'=>$data['git_count'],
                'count' => $data['count1']+$data['git_count'],
                'tcategory_id'=>$data['tcategory_id'],
            );
            $stmt = Db::name('water_ticket')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
//            return $data;
        }
            $id = $this->request->param('id/d', 0);

            $data = Db::name('water_ticket')->find($id);
            $this->assign('data', $data);
            return $this->fetch();
    }

}